
const nowTime = (new Date().getTime() / 1000);
const LOCAL_USER = "_LOCAL_USER";
const LOCAL_DATE = "_LOCAL_DATE";
const LOCAL_DATE_STRING = "LOCAL_DATE_STRING";
const LOCAL_WX = "_LOCAL_WX";
import {
    uploadFile
} from "../api/index";



function getUserInfo() {
    let userInfo = localStorage.getItem(LOCAL_USER);
    if (userInfo) {
        return JSON.parse(userInfo)
    }
    return null
}

function getToken() {
    let token = null;
    let userInfo = getUserInfo();
    if (userInfo && userInfo.expire > nowTime) {
        return userInfo["token"]
    }
    return token
}

function getUserType() {
    let type = "login";
    let userInfo = getUserInfo();
    if (userInfo && userInfo.expire > nowTime) {
        return userInfo["type"]
    }
    return type
}

function log(...msg) {
    if (process.env.NODE_ENV === 'development') {
        console.log("log--", msg)
    }
}

function isMobile(text) {
    return (/^1\d{10}$/).test(text)
}

function isWx() {
    const ua = window.navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) == 'micromessenger') {
        return true
    } else {
        return false
    }
}
function fileUpload(fileList, _data, cb) {
    let uploadNum = 0;
    let imageList = [];
    fileList.forEach(file => {
        uploadFile(file, _data).then((res) => {
            if (res.data.success) {
                imageList.push(res.data.data);
                uploadNum = uploadNum + 1;
                if (uploadNum == fileList.length) {
                    if (cb && typeof cb == "function") {
                        cb(imageList);
                    }
                }
            }
        });
    })
}

export {
    getUserInfo,
    getToken,
    getUserType,
    log,
    isMobile,
    isWx,
    fileUpload,
    LOCAL_USER,
    LOCAL_DATE,
    LOCAL_DATE_STRING,
    LOCAL_WX
}